package com.wtwd.campus.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.model.vo.SysUserVO;
import com.wtwd.campus.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zdl
 * @since 2021-06-10
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/sys-user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "查询用户分页数据", notes = "默认获取直属下级,选择同步下级后,获取所有下级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "account", value = "账号", dataType = "String")
            , @ApiImplicitParam(name = "nickName", value = "用户名称", dataType = "String")
            , @ApiImplicitParam(name = "isAllLower", value = "同步下级", dataType = "Boolean", example = "false")
            , @ApiImplicitParam(name = "orgId", value = "组织ID", dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "orgCode", value = "组织编号", dataType = "String", example = "当orgId有值时,orgCode必传")
    })
    @GetMapping("/page")
    public ResultVO<PageDataVO<SysUserVO>> queryPage(@RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "10") Integer pageSize, String account
            , String nickName, boolean isAllLower, Integer orgId, String orgCode) {
        Page<SysUserVO> page = new Page<>(pageNo, pageSize);
        SysUser sysUser = new SysUser();
        sysUser.setNickName(nickName);
        sysUser.setAccount(account);
        if (isAllLower) {
            if (StringUtils.isBlank(orgCode)) {
                SysUser user = sysUserService.getCurrentLoginUser();
                sysUser.setOrgCode(user.getOrgCode());
            } else {
                sysUser.setOrgCode(orgCode);
            }
        } else {// 查询直属下级
            if (orgId != null) {
                sysUser.setOrgId(orgId);
            } else {
                SysUser user = sysUserService.getCurrentLoginUser();
                sysUser.setOrgId(user.getOrgId());
            }
        }
        return ResultVO.ok(sysUserService.queryPage(page, sysUser));
    }

    @ApiOperation("添加用户信息")
    @DynamicParameters(name = "addUserModel", properties = {
            @DynamicParameter(name = "orgId", value = "组织ID", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "orgCode", value = "组织编号", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "account", value = "账号", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "password", value = "密码", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "nickName", value = "用户名称", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "roleId", value = "用户角色ID", dataTypeClass = Integer.class, required = true)
    })
    @ApiResponses({@ApiResponse(code = 512, message = "账号已存在")})
    @PostMapping("/user")
    public ResultVO add(@RequestBody JSONObject addUserModel) {
        SysUser sysUser = JSONObject.toJavaObject(addUserModel, SysUser.class);
        return sysUserService.createUser(sysUser);
    }

    @ApiOperation("更新用户信息")
    @DynamicParameters(name = "updateUserModel", properties = {
            @DynamicParameter(name = "userId", value = "用户id", dataTypeClass = Integer.class, required = true)
            , @DynamicParameter(name = "account", value = "账号", dataTypeClass = String.class)
            , @DynamicParameter(name = "password", value = "密码", dataTypeClass = String.class)
            , @DynamicParameter(name = "nickName", value = "用户名称", dataTypeClass = String.class)
            , @DynamicParameter(name = "roleId", value = "用户角色ID", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code = 512, message = "账号已存在")})
    @PutMapping("/user")
    public ResultVO update(@RequestBody JSONObject updateUserModel) {
        SysUser sysUser = JSONObject.toJavaObject(updateUserModel, SysUser.class);
        return sysUserService.updateUser(sysUser);
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @DeleteMapping("/user/{userId}")
    public ResultVO delete(@PathVariable Integer userId) {
        if (userId == sysUserService.getCurrentLoginUser().getUserId()) {
            return ResultVO.error(RespCode.PERMISSION_DENIED);
        }
        sysUserService.deleteById(userId);
        return ResultVO.ok();
    }
}

